package com.example.za.account.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.za.account.R;
import com.example.za.account.application.AccountApplication;
import com.example.za.account.dao.UserDao;
import com.example.za.account.utils.Constant;
import com.example.za.account.utils.T;
import com.example.za.account.view.NormalEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;


public class NameActivity extends BaseActivity {

    @BindView(R.id.et_modify_username)
    NormalEditText etModifyUsername;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name);
        ButterKnife.bind(this);
        setTitle(getString(R.string.edit_name));
        showBackwardView(true);
        etModifyUsername.setText(BmobUser.getCurrentUser().getUsername());

    }

    @Override
    protected Activity getSubActivity() {
        return this;
    }

    @OnClick(R.id.btn_modify_username)
    public void onViewClicked() {
        final String name = etModifyUsername.getText().toString().trim();
        if (!TextUtils.isEmpty(name)){
            new AsyncTask<Void,Void,Void>(){

                @Override
                protected Void doInBackground(Void... params) {
                    BmobQuery<BmobUser> query = new BmobQuery<BmobUser>();
                    query.addWhereEqualTo("username",name);
                    query.findObjects(new FindListener<BmobUser>() {
                        @Override
                        public void done(List<BmobUser> list, BmobException e) {
                            if (e != null){
                                T.showShort(NameActivity.this, getString(R.string.query_fail));
                            }else {
                                if (list != null && list.size()>0){
                                    T.showShort(NameActivity.this, getString(R.string.user_exist));
                                }else {
                                    BmobUser newUser = new BmobUser();
                                    newUser.setUsername(name);
                                    BmobUser bmobUser = BmobUser.getCurrentUser();
                                    newUser.update(bmobUser.getObjectId(), new UpdateListener() {
                                        @Override
                                        public void done(BmobException e) {
                                            if (e != null){
                                                T.showShort(NameActivity.this, getString(R.string.update_user_fail));
                                            }else {
                                                new UserDao(NameActivity.this).updateName(name, AccountApplication.sUser.getmId());
                                                T.showShort(NameActivity.this, getString(R.string.update_user_succeed));
                                                Intent intent = new Intent();
                                                intent.putExtra(Constant.NEW_USERNAME,name);
                                                setResult(Activity.RESULT_OK,intent);
                                                finish();
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    });
                    return null;
                }

            }.execute();
        }else {
            T.showShort(this, getString(R.string.input_username));
        }
    }
}
